﻿Public Class FrmLapPhieuNhap

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        dgvSanPham.Rows.Add(cboSanPham.SelectedValue, cboSanPham.Text, txtSoLuong.Text, cboNhaCungCap.SelectedValue, cboNhaCungCap.Text, txtDonGia.Text)

    End Sub

    Private Sub FrmLapPhieuNhap_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cboSanPham.DataSource = SanPhamDAO.SelectAll
        cboSanPham.ValueMember = "IDMaSP"
        cboSanPham.DisplayMember = "TenSp"

        cboNhaCungCap.ValueMember = "IDNhaCC"
        cboNhaCungCap.DisplayMember = "TenNhaCC"
        cboNhaCungCap.DataSource = NhaCungCapDAO.SelectAll

        'cmbPhieuNhap.ValueMember = "IDMaPN"
        'cmbPhieuNhap.DisplayMember = "IDMaPN"
        'cmbPhieuNhap.DataSource = PhieuNhapDAO.SelectAll

        LoadLenLuoi()

    End Sub

    Private Sub btnLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuu.Click
        Dim p = New PhieuNhapDTO
        p.NgayNhap = DateTime.Now

        Dim id As Integer = PhieuNhapDAO.InsertPN(p)

        For Each o As DataGridViewRow In dgvSanPham.Rows
            Dim a = New CTPhieuNhapDTO
            a.MaSanPham = o.Cells(0).Value
            a.SoLuongNhap = o.Cells(2).Value
            a.MaNhaCC = o.Cells(3).Value
            a.DonGia = o.Cells(5).Value
            a.MaPhieuNhap = id
            If Not String.IsNullOrEmpty(o.Cells(0).Value) Then
                PhieuNhapDAO.InsertCTPN(a)
            End If

        Next
        
        LoadLenLuoi()


    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()

    End Sub

    Sub LoadLenLuoi()
        Dim ds As DataTable
        ds = PhieuNhapDAO.SelectAll()
        dgvPhieuNhap.DataSource = ds

    End Sub

    Private Sub dgvPhieuNhap_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvPhieuNhap.SelectionChanged
        HienThiChinhSua()
    End Sub

    Private Sub dgvPhieuNhap_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPhieuNhap.CellContentClick
        LoadLenLuoi()

    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Xoa()
    End Sub

    Sub Xoa()
        Dim ctpn As CTPhieuNhapDTO = New CTPhieuNhapDTO()
        ctpn.MaCTPhieuNhap = Convert.ToInt32(txtMaCTPN.Text)
        PhieuNhapDAO.DeleteCTPN(ctpn)
        LoadLenLuoi()

    End Sub

    Sub HienThiChinhSua()
        If (dgvPhieuNhap.SelectedRows.Count > 0) Then
            txtMaCTPN.Text = dgvPhieuNhap.SelectedRows(0).Cells(0).Value.ToString()
            cboSanPham.Text = dgvPhieuNhap.SelectedRows(0).Cells(1).Value.ToString()
            txtMaPN.Text = dgvPhieuNhap.SelectedRows(0).Cells(4).Value.ToString()
            cboNhaCungCap.Text = dgvPhieuNhap.SelectedRows(0).Cells(5).Value.ToString()
            txtSoLuong.Text = dgvPhieuNhap.SelectedRows(0).Cells(2).Value.ToString()
            txtDonGia.Text = dgvPhieuNhap.SelectedRows(0).Cells(3).Value.ToString()

        End If
    End Sub

End Class